package com.huawei.hianalytics.ab.bc.cd.bc;

import android.text.TextUtils;
import com.huawei.hianalytics.ab.bc.ab.bc;
import com.huawei.hianalytics.ab.bc.kl.de;
import com.huawei.hianalytics.ab.ef.ab;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class ef implements fg {
    private List<de> ab;
    private bc bc;
    private cd cd;
    private fg de;
    private String ef = "";
    private String fg;

    public ef(String str) {
        this.fg = str;
    }

    public void ab(bc bcVar) {
        this.bc = bcVar;
    }

    public void ab(cd cdVar) {
        this.cd = cdVar;
    }

    public void ab(ab abVar) {
        this.de = abVar;
    }

    public void ab(String str) {
        if (str != null) {
            this.ef = str;
        }
    }

    public void ab(List<de> list) {
        this.ab = list;
    }

    public JSONObject cd() {
        if (this.ab == null || this.ab.size() == 0) {
            com.huawei.hianalytics.ab.bc.ef.ab.cd("EventUploadModel", "Not have actionEvent to send");
            return null;
        } else if (this.bc == null || this.cd == null || this.de == null) {
            com.huawei.hianalytics.ab.bc.ef.ab.cd("EventUploadModel", "model in wrong format");
            return null;
        } else {
            JSONObject jSONObject = new JSONObject();
            jSONObject.put("header", this.bc.cd());
            JSONObject jSONObject2 = new JSONObject();
            JSONObject cd = this.de.cd();
            cd.put("properties", this.cd.cd());
            try {
                cd.put("events_global_properties", new JSONObject(this.ef));
            } catch (JSONException e) {
                cd.put("events_global_properties", this.ef);
            }
            jSONObject2.put("events_common", cd);
            JSONArray jSONArray = new JSONArray();
            for (de cd2 : this.ab) {
                cd = cd2.cd();
                if (cd != null) {
                    jSONArray.put(cd);
                } else {
                    com.huawei.hianalytics.ab.bc.ef.ab.cd("EventUploadModel", "custom event is empty,delete this event");
                }
            }
            jSONObject2.put("events", jSONArray);
            try {
                CharSequence ab = bc.ab().ab(bc.ab.AES).ab(this.fg, de.ab(jSONObject2.toString().getBytes("UTF-8")));
                if (TextUtils.isEmpty(ab)) {
                    com.huawei.hianalytics.ab.bc.ef.ab.cd("EventUploadModel", "eventInfo encrypt failed,report over!");
                    return null;
                }
                jSONObject.put("event", ab);
                return jSONObject;
            } catch (UnsupportedEncodingException e2) {
                com.huawei.hianalytics.ab.bc.ef.ab.cd("EventUploadModel", "getBitZip(): Unsupported coding : utf-8");
                return null;
            }
        }
    }
}
